package com.example.multipledemo;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;

import java.lang.reflect.Method;

/**
 * Created by tangjiangxu on 11/10/17.
 */

public class HostActivity extends Activity{

    Resources mResources;
    AssetManager mAssetManager;
    Resources.Theme mTheme;

    public void loadResource(Context context, String dexPath) {
        try {
            mAssetManager = AssetManager.class.newInstance();
            Method addAssetPath = mAssetManager.getClass().getMethod("addAssetPath", String.class);
            addAssetPath.invoke(mAssetManager, dexPath);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Resources superRes = super.getResources();
        mResources = new Resources(mAssetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
        mTheme = mResources.newTheme();
        mTheme.setTo(super.getTheme());
    }

    @Override
    public AssetManager getAssets() {
        return mAssetManager == null? super.getAssets() : mAssetManager;
    }

    @Override
    public Resources getResources() {
        return mResources == null ? super.getResources() : mResources;
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            Method onResume = mActivityLifecircleMethods.get("onResume");
            if (onResume != null) {
                onResume.invoke(mRemoteActivity, new Object[] {});
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            Method onResume = mActivityLifecircleMethods.get("onPause");
            if (onResume != null) {
                onResume.invoke(mRemoteActivity, new Object[] {});
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
